//
//  FortuneDetailViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/5/13.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "FortuneDetailViewController.h"
#import "FortuneTableViewCell.h"
#import "ModelContent.h"
#import "CONST.h"

@interface FortuneDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *nowAmountBackview ;
    CGRect labelframe ;
}
@end

@implementation FortuneDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor] ;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    [self createTitle] ;
}

- (void)createTitle
{
    
    //当前余额背景图
    nowAmountBackview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, 70)] ;
    nowAmountBackview.backgroundColor = [UIColor whiteColor] ;
    [self.view addSubview:nowAmountBackview] ;
    
    //余额label
    UILabel *nowtitleLabel = [[UILabel alloc] init] ;
    nowtitleLabel.text = @"当前余额" ;
    
    nowtitleLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    CGSize titleLength = [self lengthwithLabel:nowtitleLabel withsize:16.0f] ;
    nowtitleLabel.frame = CGRectMake(10, 16, titleLength.width, 18.0f) ;
    nowtitleLabel.textColor = [UIColor blackColor] ;
    [nowAmountBackview addSubview:nowtitleLabel] ;
    
    //当前余额
    UILabel *nowNumLabel = [[UILabel alloc] init] ;
    nowNumLabel.text = [NSString stringWithFormat:@"%@",_detailModel.totalamount] ;
    nowNumLabel.textColor = [UIColor orangeColor] ;
    nowNumLabel.font = [UIFont fontWithName:@"AvenirLTStd-Medium" size:28.0f];
    CGSize nowNumLength = [self lengthwithLabel:nowNumLabel withsize:28.0f] ;
    
    UILabel *nowWordLabel = [[UILabel alloc] init] ;
    nowWordLabel.text = @"元" ;
    nowWordLabel.textColor = [UIColor orangeColor] ;
    nowWordLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f];
    CGSize nowWordLength = [self lengthwithLabel:nowWordLabel withsize:16.0f] ;
    
    nowNumLabel.frame = CGRectMake((__kScreenWidth - nowNumLength.width -nowWordLength.width-16)/2, (nowAmountBackview.frame.size.height-28.0f)/2, nowNumLength.width+10, 30.0f) ;
    nowWordLabel.frame = CGRectMake(nowNumLabel.frame.origin.x+nowNumLabel.frame.size.width+6, nowNumLabel.frame.origin.y+6, nowWordLength.width, 18.0f) ;
    [nowAmountBackview addSubview:nowNumLabel] ;
    [nowAmountBackview addSubview:nowWordLabel] ;
    
    UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, nowAmountBackview.frame.origin.y+nowAmountBackview.frame.size.height, __kScreenWidth, 3) ];
    [lineView setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [self.view addSubview:lineView] ;
    
    //近期流水label
    UILabel *jinqiLabel = [[UILabel alloc] init] ;
    jinqiLabel.text = @"近期流水:" ;
    
    jinqiLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    CGSize jinqiLength = [self lengthwithLabel:jinqiLabel withsize:16.0f] ;
    jinqiLabel.frame = CGRectMake(10, lineView.frame.origin.y + lineView.frame.size.height+10, jinqiLength.width, 18.0f) ;
    jinqiLabel.textColor = [UIColor blackColor] ;
    [self.view addSubview:jinqiLabel] ;
    
    UIImageView *lineView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, jinqiLabel.frame.origin.y + jinqiLabel.frame.size.height + 10, __kScreenWidth, 0.5) ];
    [lineView1 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [self.view addSubview:lineView1] ;
    
    [self createLabel:@"日期" atIndex:0 withframe:lineView1.frame color:[UIColor blackColor]] ;
    [self createLabel:@"操作" atIndex:1 withframe:lineView1.frame color:[UIColor blackColor]] ;
    [self createLabel:@"工单数" atIndex:2 withframe:lineView1.frame color:[UIColor blackColor]] ;
    [self createLabel:@"金额" atIndex:3 withframe:lineView1.frame color:[UIColor orangeColor]] ;

    UIImageView *lineView2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, labelframe.origin.y + labelframe.size.height + 6, __kScreenWidth - 20, 0.5) ];
    [lineView2 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [self.view addSubview:lineView2] ;
    CGRect tabelviewFrame = CGRectMake(0, lineView2.frame.origin.y+lineView2.frame.size.height, __kScreenWidth, __kScreenHeight - lineView2.frame.origin.y+lineView2.frame.size.height-66) ;
    [self createTableView:tabelviewFrame] ;
}

#pragma mark - 创建tableview
- (void)createTableView:(CGRect) frame
{
    UITableView *fortuneDetailList = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain] ;
    fortuneDetailList.separatorStyle = UITableViewCellSeparatorStyleNone ;
    fortuneDetailList.showsVerticalScrollIndicator = NO;
    fortuneDetailList.scrollEnabled = YES ;
    fortuneDetailList.delegate = self ;
    fortuneDetailList.dataSource = self ;
    [self.view addSubview:fortuneDetailList] ;
}

#pragma mark - 创建label
- (void)createLabel:(NSString *)title atIndex:(NSInteger) index withframe:(CGRect) frame color:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init] ;
    label.text = title ;
    
    label.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
    CGFloat length = __kScreenWidth / 4.0 ;
    label.frame = CGRectMake(length * index, frame.origin.y + frame.size.height+6, length, 18.0f) ;
    label.textColor = color ;
    label.textAlignment = NSTextAlignmentCenter ;
    [self.view addSubview:label] ;
    labelframe = label.frame ;
}

- (CGSize)lengthwithLabel:(UILabel *)lable withsize:(CGFloat) size
{
    NSDictionary *attribute1 = @{NSFontAttributeName: [UIFont fontWithName:@"AvenirLTStd-Light" size:size]};
    CGSize DescSize = [lable.text boundingRectWithSize:CGSizeMake(MAXFLOAT, size) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    return DescSize ;
}

#pragma mark - TableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _detailModel.content.count ;
   // return 10 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FortuneTableViewCell *cell = [FortuneTableViewCell cellWithTableView:tableView] ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone ;
    NSMutableArray *modelArray = [[NSMutableArray alloc] init] ;
    if (_detailModel.content.count != 0) {
        for (NSDictionary *dic in _detailModel.content) {
            ModelContent *contentData = [[ModelContent alloc] init] ;
            [contentData setValuesForKeysWithDictionary:dic] ;
            [modelArray addObject:contentData] ;
        }
        [cell config:modelArray[indexPath.row]] ;

    }
    
    return cell ;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50 ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
